package com.sscaipiao.android.activity.Base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.LotteryEntry;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.DateUtil;
import com.sscaipiao.android.utils.StringUtil;

import java.util.List;

/**
 * Created by ppona_000 on 13-12-30.
 */
public class NormalActivity extends BaseActivity implements View.OnClickListener,Handler.Callback {
	protected String date;
	protected String leftTime;
	protected List<String> history;
	protected List<String> lostValues;
	protected String mLottCode;
	protected int mRandomType = Constants.RANDOM_TYPE_IN_ALL;
	private TextView mTitleText;
	private Button mTitleBack;
	private Button mTitleArrow;
	private ScrollView mPartMain;
	private ViewGroup mPartHistory;
	private ViewGroup mPartBottomBar;
	private ViewGroup mPartRadom;
	private TextView mTVSeason;
	private TextView mTVBallNum1;
	private TextView mTVBallNum2;
	private TextView mTVDeadLine;
	private ImageView mShowHisory;
	private TextView mTVMoney;
	private Button mTVConfirm;
	private ImageView mIMUndo;
	private TextView mTVInfo;
	private Button mBtnRadom;
	private Handler mHandler ;

//	private onClickCallback mCallback ;
//	public interface onClickCallback{
//		void onBaseViewClick(View view);
//	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.normal_activity);
		mTitleText = (TextView) findViewById(R.id.title_text);
		mTitleBack = (Button) findViewById(R.id.title_back);
		mTitleArrow = (Button) findViewById(R.id.title_arrow);
		mTitleBack.setOnClickListener(this);
		mTitleArrow.setOnClickListener(this);
		mPartMain = (ScrollView) findViewById(R.id.part_main);
		mPartHistory = (ViewGroup) findViewById(R.id.part_history);
		mPartBottomBar = (ViewGroup) findViewById(R.id.part_bottom_bar);
		mPartRadom = (ViewGroup) findViewById(R.id.part_radom);
		mTVSeason = (TextView) findViewById(R.id.tv_season);
		mTVBallNum1 = (TextView) findViewById(R.id.tv_ball_num_1);
		mTVBallNum2 = (TextView) findViewById(R.id.tv_ball_num_2);
		mTVDeadLine = (TextView) findViewById(R.id.tv_dead_line);
		mShowHisory = (ImageView) findViewById(R.id.btn_show_history);
		mShowHisory.setOnClickListener(this);
		mTVMoney = (TextView) findViewById(R.id.tv_money);
		mTVConfirm = (Button) findViewById(R.id.btn_confirm);
		mTVConfirm.setOnClickListener(this);
		mIMUndo = (ImageView) findViewById(R.id.im_undo);
		mIMUndo.setOnClickListener(this);
		mTVInfo = (TextView) findViewById(R.id.tv_info);
		mBtnRadom = (Button) findViewById(R.id.btn_radom);
		mBtnRadom.setOnClickListener(this);

		mHandler = new Handler(this);

	}

	public void showLottHistory(Class hisName) {
		String code = null;
		for (LotteryEntry e : Constants.LOTT_ARRAY) {
			if (e.getHisActivity() == hisName) {
				 code = e.getCode();
			}
		}
		if (code == null) {
			Toast.makeText(this, "彩票历史功能未提供", Toast.LENGTH_SHORT).show();
			return;
		}
		Intent intent = new Intent(this, hisName);
		intent.putExtra("code", code);
		startActivity(intent);
	}
	public void showLottPayActivity(Class Name) {
		Intent intent = new Intent(this, Name);
		Bundle b = new Bundle();
		b.putSerializable(Constants.USER, user);
		b.putSerializable("date", date);
		b.putSerializable("leftTime", leftTime);
		intent.putExtras(b);
		startActivity(intent);
	}

	public void setupHeadBar(String code) {
		mLottCode = code;
		showProgressDialog();
		LoadSeasonThread mLST = new LoadSeasonThread(mHandler);
		mLST.start();
		LoadDeadLineThread mLDT = new LoadDeadLineThread(mHandler);
		mLDT.start();

	}
	/**
	 * @deprecated Use {@link #setupHeadBar}
	 */
    @Deprecated
	public void setLottInformation(Intent intent, String code) {
		try {
			history = CaipiaoServiceHelper.getLottHistoryList(this, user, code);
		} catch (Exception e) {
		}
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (history != null && history.size() > 0) {
					Log.v("history.get(0)", history.get(0));
					String[] latestPrice = history.get(0).split(Constants.STRING_SPLITE_COMPLEX);
					setSeason("第" + latestPrice[0] + "期 :");
					setBallNum(latestPrice[1], latestPrice[2]);
				}
			}
		});
	}

	// "periodNo":"2014007","currPool":0,"betCount":0,"dLineTime":"2014-01-15 19:57:00.0"}
	public void setupLottInformation(String code) {
		String info = null;
		try {
			info = CaipiaoServiceHelper.getLottery(this, user, code);
		} catch (Exception e) {
		}
		final String finalInfo = info;
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (StringUtil.isNotEmptyString(finalInfo)) {
					String[] array = finalInfo.split(Constants.STRING_SPLITE_COMPLEX);
					setDeadLine("距 " + array[0] + "期截止: " + DateUtil.getCountdownDateStr(DateUtil.getDate(array[3])));
				}
			}
		});

	}

	public boolean getLottLostValues_DoubleBall() {
		try {
			lostValues = CaipiaoServiceHelper.getLottLostValues_DoubleBall(this, user, "ssq");
			updateToken();
			return true;

		} catch (Exception e) {

		}
		return false;
	}
	public boolean getLottLostValues() {
		try {
			lostValues = CaipiaoServiceHelper.getLottLostValues_DoubleBall(this, user, mLottCode);
			return true;

		} catch (Exception e) {

		}
		return false;
	}
	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	protected void onStop() {
		super.onStop();

	}

	public void updateToken() {
		Intent intent = new Intent();
		intent.putExtra(Constants.TOKEN, user.getToken());
		setResult(0, intent);
	}

	public void setTitleText(String string) {
		if (mTitleText != null) {
			mTitleText.setText(string);
		}
	}
	public void setTitleText(int id) {
		if (mTitleText != null) {
			String title = getResources().getString(id);
			setTitleText(title);
		}
	}

	public void setupUI(boolean bShowBack, boolean bShowArrow,boolean bShowHis,boolean bShowRadom, boolean bShowBottom) {
		mTitleBack.setVisibility(bShowBack == true ? View.VISIBLE : View.GONE);
		mTitleArrow.setVisibility(bShowArrow == true ? View.VISIBLE : View.GONE);
		mPartHistory.setVisibility(bShowHis == true ? View.VISIBLE : View.GONE);
		mPartBottomBar.setVisibility(bShowBottom == true ? View.VISIBLE : View.GONE);
		mPartRadom.setVisibility(bShowRadom == true ? View.VISIBLE : View.GONE);
		if (bShowBottom == false) {
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mPartMain.getLayoutParams();
			lp.setMargins(lp.leftMargin,lp.topMargin,lp.rightMargin,0);
			mPartMain.setLayoutParams(lp);
		}
	}

	public void addContainerMain(int container_id) {
		LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(container_id,null);
		if (layout != null) {
			mPartMain.addView(layout);
		}
	}
//	public void setCallback(onClickCallback cb) {
//		mCallback = cb;
//	}

	@Override
	public void onClick(View v) {
		if (v == mTitleBack) {
			finish();
		}
		if (v == mTitleArrow) {

		}
		if (v == mShowHisory) {

		}
		if (v == mIMUndo) {

		}
		if (v == mTVConfirm) {

		}

	}

	public void setSeason(String season) {
		mTVSeason.setText(season);
	}

	public void setBallNum(String num1, String num2) {
		mTVBallNum1.setText(num1);
		mTVBallNum2.setText(num2);

	}

	public void setDeadLine(String time) {
		mTVDeadLine.setText(time);
	}

	public void setMoney(String money) {
		mTVMoney.setText(money);
	}

	public void setInfo(String info) {
		mTVInfo.setText(info);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			case Constants.MESSAGE_WORK_DOWN:
				closeProgressDailog();
				break;
			case Constants.MESSAGE_UPDATE_HISTORY:
				if (history != null && history.size() > 0) {
					Log.v("history.get(0)", history.get(0));
					String[] latestPrice = history.get(0).split(Constants.STRING_SPLITE_COMPLEX);
					setSeason("第" + latestPrice[0] + "期 :");
					setBallNum(latestPrice[1], latestPrice[2]);
				}
				break;
			case Constants.MESSAGE_UPDATE_DEADLINE:
				String info = (String) msg.obj;
				if (StringUtil.isNotEmptyString(info)) {
					String[] array = info.split(Constants.STRING_SPLITE_COMPLEX);
					setDeadLine("距 " + array[0] + "期截止: " + DateUtil.getCountdownDateStr(DateUtil.getDate(array[3])));
				}
				break;
			case Constants.MESSAGE_SHOW_TOAST:
				Toast.makeText(this, (CharSequence) msg.obj,Toast.LENGTH_SHORT).show();
			default:
		}

		return true;
	}

	public class LoadSeasonThread extends Thread {
		Handler mHandler;
		public LoadSeasonThread(Handler handler) {
			mHandler = handler;
		}

		@Override
		public void run() {
			String info = null;
			try {
				history = CaipiaoServiceHelper.getLottHistoryList(NormalActivity.this, user, mLottCode);
				mHandler.obtainMessage(Constants.MESSAGE_UPDATE_HISTORY).sendToTarget();

			} catch (Exception e) {
				mHandler.obtainMessage(Constants.MESSAGE_SHOW_TOAST,"获取上期彩票信息失败").sendToTarget();
			}
		}
	}
	public class LoadDeadLineThread extends Thread {
		Handler mHandler;
		public LoadDeadLineThread(Handler handler) {
			mHandler = handler;
		}

		@Override
		public void run() {
			String info = null;
			try {
				info = CaipiaoServiceHelper.getLottery(NormalActivity.this, user, mLottCode);
				mHandler.obtainMessage(Constants.MESSAGE_UPDATE_DEADLINE,info).sendToTarget();

			} catch (Exception e) {
				mHandler.obtainMessage(Constants.MESSAGE_SHOW_TOAST,"获取彩票信息失败").sendToTarget();
			}
			mHandler.obtainMessage(Constants.MESSAGE_WORK_DOWN).sendToTarget();

		}
	}
}